package com.inspectionwork.web.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inspectionwork.common.base.BaseService;
import com.inspectionwork.model.Comment;
import com.inspectionwork.model.safetyknowledge.SafetyKnowledge;
import com.inspectionwork.model.safetyknowledge.TextType;
import com.inspectionwork.support.model.RespEntity;
import com.inspectionwork.support.mybatisplus.QueryWrapper;
import com.inspectionwork.support.utils.requestutil.HttpRequestUtil;
import com.inspectionwork.web.mapper.CommentMapper;
import com.inspectionwork.web.mapper.safetyknowledge.SafetyKnowledgeMapper;
import com.inspectionwork.web.mapper.safetyknowledge.TextTypeMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author: YangYu
 * @ClassName: SafetyKnowledgeService
 * @Date: Created in 13:59 2022/6/23
 * @Description: 安全知识
 */
@Service
public class SafetyKnowledgeService extends BaseService {

    @Resource
    private SafetyKnowledgeMapper safetyKnowledgeMapper;
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private TextTypeMapper textTypeMapper;
    /**
     * @Author YangYu
     * @Description 安全知识分页列表
     * @Date Created in 14:05 2022/6/23
     * @Param [jsonParam]
     */

    public RespEntity getSafetyKnowledgeListPage(JSONObject jsonParam){
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //调用token验证接口
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String homeId = token.getString("home_id");
        String userType = token.getString("user_type");
        if (!jsonParam.containsKey("source")){
            jsonParam.put("orgId",homeId);
        }
        Page<SafetyKnowledge> page = new Page<>(jsonParam.getInt("page"), jsonParam.getInt("limit"));
        IPage<SafetyKnowledge> pageList = safetyKnowledgeMapper.getSafetyKnowledgeListPage(page, jsonParam);
        Map<String, Object> data = new HashMap<>();
        //总条数
        data.put("totalSize", pageList.getTotal());
        //每页展示多少条
        data.put("pageSize", pageList.getSize());
        //当前页
        data.put("currentPage", pageList.getCurrent());
        //数据
        data.put("Items", pageList.getRecords());
        //总页数
        data.put("pages", pageList.getPages());
        //当前页多少条
        data.put("currentSize", pageList.getRecords().size());
        return RespEntity.success(data);
    }

    /**
     * @Author YangYu
     * @Description 新增文章
     * @Date Created in 11:10 2022/7/6
     * @Param [jsonParam]
     */

    public RespEntity addOrUpdateArticle(JSONObject jsonParam){
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //调用token验证接口
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String homeId = token.getString("home_id");
        String userType = token.getString("user_type");

        if (!jsonParam.containsKey("id")){
            jsonParam.put("id",UUID.randomUUID().toString());
        }else {
            safetyKnowledgeMapper.deleteById(jsonParam.getStr("id"));
        }
        if (!homeId.equals("001e7548-8735-4b31-b30b-c9361a62c102")){
            jsonParam.put("state","3");
        }else {
            jsonParam.put("state","1");
        }
        jsonParam.put("homeId",homeId);
        com.alibaba.fastjson.JSONObject orgIds = makeDataForToken(jsonParam, "systemsetup", "config", "orgAllLists", "systemsetup");
        String orgId = orgIds.getString("orgIds");
        jsonParam.put("orgIds",orgId);
        save(jsonParam);
        return RespEntity.success();
    }
    public void save(JSONObject jsonParam){
        SafetyKnowledge safetyKnowledge = new SafetyKnowledge();
        safetyKnowledge.setId(jsonParam.getStr("id"));
        safetyKnowledge.setTitle(jsonParam.getStr("title"));
        safetyKnowledge.setIntroduction(jsonParam.getStr("introduction"));
        safetyKnowledge.setContent(jsonParam.getStr("content"));
        safetyKnowledge.setSource(jsonParam.getStr("source"));
        safetyKnowledge.setKnowledgeType(jsonParam.getStr("knowledgeTypeId"));
        safetyKnowledge.setPath(jsonParam.getStr("path"));
        safetyKnowledge.setCover(jsonParam.getStr("cover"));
        safetyKnowledge.setState(jsonParam.getStr("state"));
        Date date = DateUtil.date();
        safetyKnowledge.setCreationTime(date);
        safetyKnowledge.setTime(date);
        safetyKnowledge.setPublisher(jsonParam.getStr("publisher"));
        safetyKnowledge.setOrgIds(jsonParam.getStr("orgIds"));
        safetyKnowledgeMapper.insert(safetyKnowledge);
    }


    /**
     * @Author YangYu
     * @Description 删除文章操作
     * @Date Created in 14:02 2022/7/6
     * @Param [jsonParam]
     */

    public RespEntity delArticle(JSONObject jsonParam){
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //调用token验证接口
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String homeId = token.getString("home_id");
        String userType = token.getString("user_type");
        //校验ID是否为空
        if (StrUtil.isBlank(jsonParam.getStr("id"))) {
            return RespEntity.fail("ID不能为空!");
        }
        String[] array = jsonParam.getStr("id").split(",");
        safetyKnowledgeMapper.batchDelete(array,"safety_knowledge");
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 文章操作
     * @Date Created in 14:14 2022/7/6
     * @Param [jsonParam]
     */

    public RespEntity articleOperation(JSONObject jsonParam){
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //调用token验证接口
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String homeId = token.getString("home_id");
        String userType = token.getString("user_type");
        //校验ID是否为空
        if (StrUtil.isBlank(jsonParam.getStr("id"))) {
            return RespEntity.fail("ID不能为空!");
        }
        String[] array = jsonParam.getStr("id").split(",");
        String state = jsonParam.getStr("state");
        List<SafetyKnowledge> safetyKnowledgeList = new ArrayList<>();
        for (String id : array){
            SafetyKnowledge safetyKnowledge = new SafetyKnowledge();
            safetyKnowledge.setId(id);
            safetyKnowledgeList.add(safetyKnowledge);
        }
        //批量更新
        safetyKnowledgeMapper.batchUpdate(safetyKnowledgeList,state,DateUtil.now());
        return RespEntity.success();
    }
    /**
     * @Author YangYu
     * @Description 安全知识评论分页列表
     * @Date Created in 14:05 2022/6/23
     * @Param [jsonParam]
     */

    public RespEntity getCommentListPage(JSONObject jsonParam){
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //调用token验证接口
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String homeId = token.getString("home_id");
        String userType = token.getString("user_type");
        Page<Comment> page = new Page<>(jsonParam.getInt("page"), jsonParam.getInt("limit"));
        IPage<Comment> pageList = commentMapper.getCommentListPage(page, jsonParam);
        Map<String, Object> data = new HashMap<>();
        //总条数
        data.put("totalSize", pageList.getTotal());
        //每页展示多少条
        data.put("pageSize", pageList.getSize());
        //当前页
        data.put("currentPage", pageList.getCurrent());
        //数据
        data.put("Items", pageList.getRecords());
        //总页数
        data.put("pages", pageList.getPages());
        //当前页多少条
        data.put("currentSize", pageList.getRecords().size());
        return RespEntity.success(data);
    }

    /**
     * @Author YangYu
     * @Description 新增评论
     * @Date Created in 14:44 2022/6/23
     * @Param [jsonParam]
     */

    public RespEntity addComment(JSONObject jsonParam){
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //调用token验证接口
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        Comment comment = new Comment();
        comment.setId(UUID.randomUUID().toString());
        comment.setKnowledgeId(jsonParam.getStr("knowledgeId"));
        comment.setContent(jsonParam.getStr("content"));
        comment.setUserName(jsonParam.getStr("userName"));
        comment.setTime(DateUtil.date());
        commentMapper.insert(comment);
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 删除评论
     * @Date Created in 14:34 2022/7/6
     * @Param [jsonParam]
     */

    public RespEntity delComment(JSONObject jsonParam){
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //调用token验证接口
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        //校验ID是否为空
        if (StrUtil.isBlank(jsonParam.getStr("id"))) {
            return RespEntity.fail("ID不能为空!");
        }
        String[] array = jsonParam.getStr("id").split(",");
        safetyKnowledgeMapper.batchDelete(array,"comment");
        return RespEntity.success();
    }
    /**
     * @Author YangYu
     * @Description 图文类型列表
     * @Date Created in 10:51 2022/7/6
     * @Param [jsonParam]
     */

    public RespEntity textTypeList(JSONObject jsonParam){
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //调用token验证接口
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        List<TextType> textTypeList = textTypeMapper.selectList(new QueryWrapper<>());
        return RespEntity.success(textTypeList);
    }

    /**
     * @Author YangYu
     * @Description 新增图文类型
     * @Date Created in 10:34 2022/7/6
     * @Param [jsonParam]
     */

    public RespEntity addOrUpdateTextType(JSONObject jsonParam){
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //调用token验证接口
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));

        TextType textType = new TextType();
        //包含id为更新
        if (!jsonParam.containsKey("id")){
            textType.setId(UUID.randomUUID().toString());
            textType.setName(jsonParam.getStr("name"));
        }else {
            //先删除再新建
            textTypeMapper.deleteById(jsonParam.getStr("id"));
            textType.setId(jsonParam.getStr("id"));
            textType.setName(jsonParam.getStr("name"));
        }
        textTypeMapper.insert(textType);
        return RespEntity.success();
    }
    /**
     * @Author YangYu
     * @Description 分类详情
     * @Date Created in 10:56 2022/7/6
     * @Param [jsonParam]
     */

    public RespEntity detailsTextType(JSONObject jsonParam){
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //调用token验证接口
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        TextType textType = textTypeMapper.selectById(jsonParam.getStr("id"));
        return RespEntity.success(textType);
    }
    /**
     * @Author YangYu
     * @Description 删除分类
     * @Date Created in 10:56 2022/7/6
     * @Param [jsonParam]
     */

    public RespEntity delTextType(JSONObject jsonParam){
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //调用token验证接口
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));

        //检验ID是否为空
        if (StrUtil.isNotBlank(jsonParam.getStr("id"))){
            textTypeMapper.deleteById(jsonParam.getStr("id"));
        }else {
            return RespEntity.fail("ID不能为空");
        }
        return RespEntity.success();
    }
}
